import { createRouter, createWebHistory } from 'vue-router';

// 页面组件
const Dashboard = () => import('../views/Dashboard.vue');
const ArticleList = () => import('../views/ArticleList.vue');
const ArticleCreate = () => import('../views/ArticleCreate.vue');
const ArticleEdit = () => import('../views/ArticleEdit.vue');
const CategoryList = () => import('../views/CategoryList.vue');
const TagList = () => import('../views/TagList.vue');
const PersonalInfo = () => import('../views/PersonalInfo.vue');
const Login = () => import('../views/Login.vue');
const ImageBed = () => import('../views/ImageBed.vue');

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // 登录页面（默认页面）
    {
      path: '/',
      name: 'login',
      component: Login,
      meta: {
        title: '登录',
        requiresAuth: false
      }
    },
    
    // 仪表盘
    {
      path: '/dashboard',
      name: 'dashboard',
      component: Dashboard,
      meta: {
        title: '仪表盘',
        requiresAuth: true
      }
    },
    
    // 文章管理
    {
      path: '/articles',
      name: 'articleList',
      component: ArticleList,
      meta: {
        title: '文章列表',
        requiresAuth: true
      }
    },
    
    // 新增文章
    {
      path: '/articles/create',
      name: 'articleCreate',
      component: ArticleCreate,
      meta: {
        title: '新增文章',
        requiresAuth: true
      }
    },
    
    // 编辑文章
    {
      path: '/articles/edit/:id',
      name: 'articleEdit',
      component: ArticleEdit,
      meta: {
        title: '编辑文章',
        requiresAuth: true
      }
    },
    
    // 分类管理
    {
      path: '/categories',
      name: 'categoryList',
      component: CategoryList,
      meta: {
        title: '分类管理',
        requiresAuth: true
      }
    },
    
    // 标签管理
    {
      path: '/tags',
      name: 'tagList',
      component: TagList,
      meta: {
        title: '标签管理',
        requiresAuth: true
      }
    },
    
    // 个人信息设置
    {
      path: '/personal-info',
      name: 'personalInfo',
      component: PersonalInfo,
      meta: {
        title: '个人信息设置',
        requiresAuth: true
      }
    },
    
    // 系统设置
    {
      path: '/system/settings',
      name: 'systemSettings',
      component: () => import('../views/SystemSettings.vue'),
      meta: {
        title: '系统设置',
        requiresAuth: true
      }
    },
    
    // 图床管理
    {
      path: '/image-bed',
      name: 'imageBed',
      component: ImageBed,
      meta: {
        title: '图床管理',
        requiresAuth: true
      }
    },

    // 登录页面重定向
    {
      path: '/login',
      redirect: '/'
    }
  ]
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - Gulu Blog Admin`;
  }

  const token = localStorage.getItem('adminToken');
  const isAuthenticated = !!token;

  // 如果已登录，访问登录页时重定向到仪表盘
  if (to.name === 'login' && isAuthenticated) {
    next({ name: 'dashboard' });
    return;
  }

  // 检查是否需要登录
  if (to.meta.requiresAuth) {
    if (!isAuthenticated) {
      // 保存目标路由，登录后可以跳转回来
      sessionStorage.setItem('redirectPath', to.fullPath);
      next({ name: 'login' });
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
